/******************************************************************************/
/*  mb_func_other.c                                                           */
/*                                                                            */
/*  FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. */
/*  Copyright (c) 2006 Christian Walter <wolti@sil.at>                        */
/*  All rights reserved.                                                      */
/******************************************************************************/

#include "data_types.h"

#include "port\port.h"
#include "mbconfig.h"
#include "mb.h"
#include "mbconfig.h"
#include "mbframe.h"
#include "mbproto.h"
#include "functions\mbutils.h"
#include "mbfunc.h"



#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
/******************************************************************************/
/*      Static variables                                                      */
/******************************************************************************/
static uint8_t s_mbSlaveID[MB_FUNC_OTHER_REP_SLAVEID_BUF];
static uint16_t s_mbSlaveIDLen;



//==============================================================================
// MbSetSlaveID()
//==============================================================================
eMB_ERROR_CODE MbSetSlaveID(const uint8_t _slaveID,
                            const bool_t _xIsRunning,
                            uint8_t const * _pAdditional,
                            const uint16_t _additionalLen)
{
    eMB_ERROR_CODE status = MB_ENOERR;

    /*  The first byte and second byte in the buffer is reserved for
     *  the parameter _slaveID and the running flag. The rest of
     *  the buffer is available for additional data.
     */
    if (_additionalLen + 2 < MB_FUNC_OTHER_REP_SLAVEID_BUF) {
        s_mbSlaveIDLen = 0;
        s_mbSlaveID[s_mbSlaveIDLen++] = _slaveID;
        s_mbSlaveID[s_mbSlaveIDLen++] = (uint8_t)(_xIsRunning ? 0xFF : 0x00);
        
        if (_additionalLen > 0) {
            memcpy(&s_mbSlaveID[s_mbSlaveIDLen], _pAdditional, (size_t)_additionalLen);
            s_mbSlaveIDLen += _additionalLen;
        };
    } 
    else {
        status = MB_ENORES;
    };

    return status;
}
//==============================================================================
//==============================================================================


//==============================================================================
// MbFuncReportSlaveID()
//==============================================================================
eMB_EXCEPTION MbFuncReportSlaveID(uint8_t * _pFrame, uint16_t * _pLen)
{
    memcpy(&_pFrame[MB_PDU_DATA_OFF], &s_mbSlaveID[0], (size_t)s_mbSlaveIDLen);
    *_pLen = (uint16_t)(MB_PDU_DATA_OFF + s_mbSlaveIDLen);
    return MB_EX_NONE;
}
//==============================================================================
//==============================================================================
#endif